using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Xml;

using PlugNT.Common;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Custom;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin
{
	public partial class MainMenu : AdminPager
	{
        protected string pluginDefault = "";
        protected string pluginChildren = "";
        protected string subMainMenu = "";
		protected void Page_Load(object sender, EventArgs e)
		{

            this.CheckPower();

            if (!IsPostBack)
            {
                LoadPlugs();

                if (this.adminer.sub_login)
                {
                    subMainMenu = SysModel.GetMenuJson(this.adminer.sys_model_id);
                }
                if ((Request["model"] as string) == "single")
                {
                    subMainMenu = SysModel.GetMenuJson(this.adminer.sys_model_id);
                }
            }
		}


        private void LoadPlugs()
        {

            StringBuilder sb = new StringBuilder();
            if (File.Exists(WebConfig.ConfigPath + "plugin.config"))
            {
                int nCount = 0;
                int fCount = 0;
                int kCount = 0;
                bool nextFlag = false;
                bool first = true;
                XmlDocument xml = new XmlDocument();
                xml.Load(WebConfig.ConfigPath + "plugin.config");
                XmlNodeList controls = xml.SelectNodes("/configs/plugitem");
                foreach (XmlNode control in controls)
                {
                    XmlNode plug = control.SelectSingleNode("plug");
                    if (plug.Attributes["setuped"].Value.ToLower() == "true")
                    {
                        XmlNode menu = control.SelectSingleNode("backstage/menu");
                        foreach (XmlNode url in menu.ChildNodes)
                        {
                            if (nextFlag)
                            {
                                sb.AppendLine(", ");
                            }
                            else
                            {
                                nextFlag = true;
                            }
                            kCount++;
                            XmlAttribute u_switchmodel = url.Attributes["switchmodel"];
                            XmlAttribute u_name = url.Attributes["name"];
                            if (u_switchmodel != null && u_name != null)
                            {
                                string prefix = (u_switchmodel.Value.ToLower() == "true") ? "smi_" : "";
                                if (first)
                                {
                                    pluginDefault = prefix + "uid1000" + kCount;
                                    first = false;
                                }
                                sb.AppendLine("\"" + prefix + "uid1000" + kCount + "\": { \"text\": \"" + u_name.Value + "\", \"url\": \"" + url.InnerText + "\" }");
                            }
                        }
                        nCount++;
                        if (nextFlag && nCount < (controls.Count-fCount))
                        {
                            sb.AppendLine(",  \"spliter1000" + nCount + "\": { \"text\": \"spliter\", \"url\": \"javascript:void();\" }");
                        }
                    }
                    else
                    {
                        fCount++;
                    }
                }
            }
            pluginChildren = sb.ToString();

        }

	}
}

